﻿using Autofac;
using Autofac.Extensions.DependencyInjection;
using Micro.Core.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Micro.Hosting
{
    public class Startup
    {
        public IWebHostEnvironment _HostEnvironment;
        public IConfiguration _Configuration;
        public IServiceCollection _ServiceCollection;



        /// <summary>
        /// 启动应用
        /// </summary>
        /// <returns></returns>
        public virtual void Run(string[] args)
        {
            WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
            //跨域配置
            builder.Services.AddCors(options =>
            {
                options.AddPolicy("any",
                builder => builder.AllowAnyHeader()
                .AllowAnyMethod()
                .AllowAnyOrigin());
            });
            builder.WebHost.UseDefaultServiceProvider(opt => { opt.ValidateOnBuild = false; });
            builder.InitBuilder();
            //绑定URL

            this._HostEnvironment = builder.Environment;
            this._Configuration = builder.Configuration;
            this._ServiceCollection = builder.Services;

            this.ConfigureServices(_ServiceCollection);
            //6.0.0版本无效，将在 6.0.3后修复
            //builder.WebHost.UseUrls("http://localhost:5400");
            builder.UseHostBuild();

            var app = builder.Build();
            app.UseCors("any");//跨域
            app.UseCustomUrls();
            this.Configure(app);

            app.Run();
        }




        public virtual void ConfigureServices(IServiceCollection services)
        {
            // ASP.NET HttpContext dependency
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            AddUserServices(services);
            services.AddControllers().AddMvcMicro();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            services.AddEndpointsApiExplorer();
            //services.AddSwaggerGen();

            services.AddMicroBuilder(builder =>
            {
                builder.AddPack<LoggerPack>()
                .AddPack<ModulesPack>()
                .AddPack<ServiceDiscoveryPack>()
                .AddPack<SwaggerPack>()
                .AddPack<SqlSugarPack>()
                .AddPack<AgileConfigPack>()
                .AddPack<DDDPack>()
                .AddPack<JobPack>()
                .AddPack<MiniProfilerPack>()
                //.AddPack<IOCPack>()
                .AddPack<GrpcPack>()
                //.AddPack<RefitPack>()
                ;
            });


        }

        public virtual void Configure(WebApplication app)
        {
            if (app.Environment.IsDevelopment())
            {
                // 在开发环境中，使用异常页面，这样可以暴露错误堆栈信息，所以不要放在生产环境。
                app.UseDeveloperExceptionPage();
            }
            // Configure the HTTP request pipeline.
            //if (app.Environment.IsDevelopment())
            //{
            //    app.UseSwagger();
            //    app.UseSwaggerUI();
            //}

            app.UseAuthorization();

            app.MapControllers();

            app.UseMicro();

        }

        protected virtual void AddUserServices(IServiceCollection services)
        {
            services.AddScoped<BaseAccount>();

        }
    }
}
